package com.prashantdctm.paidemail;

import java.io.File;
import java.io.IOException;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.time.StopWatch;
import java.util.logging.Logger;

import com.gargoylesoftware.htmlunit.gae.GAEUtils;
import com.prashantdctm.utils.GaeUtil;
import com.prashantdctm.utils.HttpUtil;
import com.prashantdctm.utils.LogFile;
import com.prashantdctm.utils.XMLUtil;


public class GaeHttpServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final static boolean PROXY_ENABLED = false;
	protected static GaeWebClient webClient = null;
	private static final Logger log = Logger.getLogger(GaeHttpServlet.class.getName());
	protected static int messagesRead = 0;
	protected static String earningBefore = null;
	protected static String earningAfter = null;
	protected static StringBuffer messageLog = new StringBuffer();
	protected static StopWatch stopWatch = new StopWatch();
	protected static boolean isCron = true;
	static PrintWriter response = null;
	static LogFile localLogFile = null;
		
	//constructor
	public GaeHttpServlet() {
		
	}
	
	// This Happens Once and is Reused
    public void init(ServletConfig config) {
    	webClient = HttpUtil.getWebClient();
    }
	
    protected static void resetVariables() {
    	webClient = HttpUtil.getWebClient();
    	messagesRead = 0;
		earningBefore = "";
		earningAfter = "";
		stopWatch.reset();
		stopWatch.start();
	}
    
    protected static void resetMessageLog() {
    	messageLog = new StringBuffer();
    }
    	
    
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		response = resp.getWriter();
		//response.println(messageLog.toString());
	}
	
	protected static void initLocalLog(String logDir, boolean append) throws IOException {
		if(!GAEUtils.isGaeMode()){
			File f = new File(logDir);
	        f.mkdir();
	        localLogFile = new LogFile(f+"/GAE Report "+getDate()+".txt", append);
			customLog("Job run on: "+getDate());
		}
	}
	
	public static void customLog(String msg) {
		if (GAEUtils.isGaeMode()) {
			response.println("<div style='font-family:Courier New, courier,monospace;font-style:normal;font-size:12px;text-align:left'>");
			response.println("***********************************Custom Log***********************************");
			response.println("<br>"+msg);
			response.println("</div>");
			messageLog.append("\n"+msg);
			log.info("***********************************Custom Log***********************************");
			log.info(msg);
		} else {
			localLogFile.log(msg);
		}
	}
    
    
    public static void customException(String msg, Exception e) {
    	if (GAEUtils.isGaeMode()) {
    		response.println("<div style='font-family:Courier New, courier,monospace;font-style:normal;font-size:12px;color:red;text-align:left'>");
    		response.println("***********************************Custom Exception***********************************");
    		response.println("<br>"+msg);
    		response.println("<br>"+e.getMessage());
    		StackTraceElement[] stack = e.getStackTrace();
    		for (StackTraceElement stackElement : stack) {
    			response.println("<br>"+stackElement.toString());
    		}
    		response.println("</div>");
    		
    		//append only message in mail
    		messageLog.append("\n"+msg+" "+e.getMessage());
    	} else {
    		localLogFile.log(msg);
    		localLogFile.log(e.getMessage());
    		StackTraceElement[] stack = e.getStackTrace();
    		for (StackTraceElement stackElement : stack) {
    			localLogFile.log(stackElement.toString());
    		}
    	}
	}
    
    protected static String getDate() {
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter= new SimpleDateFormat("dd-MMM-yyyy");
		String dateNow = formatter.format(currentDate.getTime());
		return dateNow;
	}
}

